from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings

def generate_access_token(openid):
    """对传入的 openid 进行加密处理, 返回 token"""
    #创建加密对象
    serializer = Serializer(settings.SECRET_KEY, expires_in=600)
    #用加密对象对数据加密
    data = {'openid': openid}

    token = serializer.dumps(data)

    return token.decode()



def check_access_token(token):
    '''对前端传来的openid解密'''
    #创建解密对象
    serializer = Serializer(settings.SECRET_KEY, expires_in=600)
    try:
        #用对象进行解密
        data = serializer.loads(token)
        return data.get('openid')
    except Exception as e:
        print('解密超时')


